﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Sockets;

namespace SSM.DeepZoomViewer.Utils
{
    /// <summary>
    /// 파일을 다운로드 받기 위해 사용하는 클래스
    /// </summary>
    public class FileDownloader
    {
        HttpWebRequest request = null;
        HttpWebResponse response = null;
        Stream responseStream = null;
        FileStream fileStream = null;
        private string path;

        /// <summary>
        /// 파일 다운로드를 수행하는 메서드
        /// </summary>
        /// <param name="url"></param>
        /// <param name="path"></param>
        public void DownloadFile(string url, string path)
        {
            this.path = path;

            // ResponseStream을 구한다.
            try
            {
                request = (HttpWebRequest)WebRequest.Create(url);
                response = (HttpWebResponse)request.GetResponse();
                responseStream = response.GetResponseStream();

                fileStream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);

                int maxRead = 1024;
                byte[] buffer = new byte[maxRead];
                int bytesRead = 0;
                int totalBytesRead = 0;

                while ((bytesRead = responseStream.Read(buffer, 0, maxRead)) > 0)
                {
                    totalBytesRead += bytesRead;
                    fileStream.Write(buffer, 0, bytesRead);
                }
            }
            catch (Exception e)
            {
                try
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();

                        if (fileStream != null)
                            File.Delete(path);
                    }
                }
                catch (Exception ex) { }

                try
                {
                    if (request != null)
                        request.Abort();
                }
                catch (Exception ex) { }

                try
                {
                    if (responseStream != null)
                        responseStream.Close();
                }
                catch (Exception ex) { }

                try
                {
                    if (response != null)
                        response.Close();
                }
                catch (Exception ex) { }
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();

                if (responseStream != null)
                    responseStream.Close();

                if (response != null)
                    response.Close();
            }
        }

        /// <summary>
        /// 다운로드 취소
        /// </summary>
        public void CancelDownloadFile()
        {
            try
            {
                if (fileStream != null)
                {
                    fileStream.Close();

                    if (fileStream != null)
                        File.Delete(path);
                }
            }
            catch (Exception ex) { }

            try
            {
                if (request != null)
                    request.Abort();
            }
            catch (Exception ex) { }

            try {
                if (responseStream != null)
                    responseStream.Close();
            }
            catch (Exception ex) { }

            try {
                if (response != null)
                    response.Close();
            }
            catch (Exception ex) { }
        }
    }
}
